package com.terminee.activities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class TermineeResultsDisplay extends Activity {

	// view components
	private ListView definitionView;
	private Button addDefinitionButton;
	
	// extras
	private String term;
	
	// request codes
	static final int ADD_DEFINITION = 0;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.terminee_results_display);
	    
	    // get view component references
	    definitionView = (ListView) findViewById(R.id.DefinitionView);
	    addDefinitionButton = (Button) findViewById(R.id.AddDefinitionButton);
	    
	    // populate term field with passed in term if there is one
	    List<HashMap<String, String>> definitionMaps = new ArrayList<HashMap<String, String>>();
		Bundle extras = getIntent().getExtras();
	    if (extras != null) {
	    	
	    	term = extras.getString("term");
	    	if(term != null && term.length() > 0) {
	    		String title = getTitle().toString();
	    		setTitle(term +" "+ title);
	    	}
	    	
	    	List<String> definitions = extras.getStringArrayList("definitions");
	    	if(definitions.size() > 0) {
	    		// add definitions to list
	    		for(int definitionIndex=0; definitionIndex < definitions.size(); definitionIndex++)	{
	    			HashMap<String, String> map = new HashMap<String, String>();
	    			map.put("index", (definitionIndex+1) + ")");
	    			map.put("definition", definitions.get(definitionIndex));
	    			definitionMaps.add(map);
	    		}
	    	}
	    }
	    
	    // create the grid item mapping
	    String[] from = new String[] {"index", "definition"};
	    int[] to = new int[] {R.id.DefinitionIndex, R.id.Definition };
	    
	    // fill in the grid_item layout
	    SimpleAdapter adapter = new SimpleAdapter(this, definitionMaps, R.layout.terminee_results_display_item, from, to);
	    definitionView.setAdapter(adapter);
	    
	    // add definition when add definition button is clicked
		addDefinitionButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { addDefinition(term); }});
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.results_display_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case R.id.DefineMenuItem:
            addDefinition("");
            return true;
        case R.id.SearchMenuItem:
        	searchTerms();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
	
    /**
     * Go to search screen
     */
    private void searchTerms() {
    	Intent termineeLauncher = new Intent(this, TermineeLauncher.class);
 	   	startActivity(termineeLauncher);
    }
    
	/**
	 * Add another definition to term
	 * 
	 * @param term term to define will automatically be filled in if provided (optional)
	 */
	public void addDefinition(String term) {
		Intent termineeInput = new Intent(this, TermineeInput.class);
 	   	if(term != null && term.length() > 0)
 	   		termineeInput.putExtra("term", term);
 	   	startActivityForResult(termineeInput, ADD_DEFINITION);
	}
	
    @Override
	protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        if (requestCode == ADD_DEFINITION) {
            if (resultCode == RESULT_OK) {
            	// finish this activity since results need to be refreshed
            	finish();
            }
        }
    }

}
